package com.bieyang.anjia.utils;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

/**
 * 自定义HTTPClient 用于封装自己的协议信息
 * 
 * @author Administrator
 * 
 */
public class CookieHttpClient implements HttpClient {

	private HttpClient client = null;
	private static String JSESSIONID = null;

	// private static final Object lock = new Object();

	public CookieHttpClient() {
		this.client = new DefaultHttpClient();
	}

	@Override
	public HttpResponse execute(HttpUriRequest request) throws IOException,
			ClientProtocolException {
		// 向服务器发送JESSIONID
		request.removeHeaders("Cookie");
		if (JSESSIONID != null) {
			request.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);
			System.out.println("JSESSIONID=" + JSESSIONID);
		}
		HttpResponse response = null;
		// synchronized (lock) {
		response = this.client.execute(request);
		Header[] headers = response.getHeaders("Set-Cookie");
		for (Header h : headers) {
			if ("Set-Cookie".equals(h.getName())) {
				String cookie = h.getValue();
				if (cookie != null && cookie.length() > 0) {
					String[] value = h.getValue().split(";");
					for (String v : value) {
						if (v.startsWith("JSESSIONID=")) {
							// 获取服务器的JSESSIONID
							JSESSIONID = v.substring("JSESSIONID=".length());
							break;
						}
					}

				}
			}
		}
		// }
		//client.getConnectionManager().shutdown();
		return response;
	}

	@Override
	public HttpResponse execute(HttpUriRequest arg0, HttpContext arg1)
			throws IOException, ClientProtocolException {
		return this.client.execute(arg0, arg1);
	}

	@Override
	public HttpResponse execute(HttpHost arg0, HttpRequest arg1)
			throws IOException, ClientProtocolException {
		return this.client.execute(arg0, arg1);
	}

	@Override
	public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1)
			throws IOException, ClientProtocolException {
		return this.client.execute(arg0, arg1);
	}

	@Override
	public HttpResponse execute(HttpHost arg0, HttpRequest arg1,
			HttpContext arg2) throws IOException, ClientProtocolException {
		return this.client.execute(arg0, arg1);
	}

	@Override
	public <T> T execute(HttpUriRequest arg0,
			ResponseHandler<? extends T> arg1, HttpContext arg2)
			throws IOException, ClientProtocolException {
		return this.client.execute(arg0, arg1);
	}

	@Override
	public <T> T execute(HttpHost arg0, HttpRequest arg1,
			ResponseHandler<? extends T> arg2) throws IOException,
			ClientProtocolException {
		return this.client.execute(arg0, arg1, arg2);
	}

	@Override
	public <T> T execute(HttpHost arg0, HttpRequest arg1,
			ResponseHandler<? extends T> arg2, HttpContext arg3)
			throws IOException, ClientProtocolException {
		return this.client.execute(arg0, arg1, arg2, arg3);
	}

	@Override
	public ClientConnectionManager getConnectionManager() {
		return this.client.getConnectionManager();
	}

	@Override
	public HttpParams getParams() {
		return this.client.getParams();
	}

}
